//
//  UserAccount.swift
//  weibo
//
//  Created by 黄龙飞 on 15/11/25.
//  Copyright © 2015年 黄龙飞. All rights reserved.
//

import UIKit

class UserAccount: NSObject {
   /*
    access_token	string	用于调用access_token，接口获取授权后的access token。
    expires_in	string	access_token的生命周期，单位是秒数。
    remind_in	string	access_token的生命周期（该参数即将废弃，开发者请使用expires_in）。
    uid	string
    */
    //
    var access_token:String?
    //现在的时间
    var expires_in:NSTimeInterval = 0 {
        didSet{
        //过期日期计算
            expiresDate = NSDate(timeIntervalSinceNow: expires_in)
        }
    }
    //过期时间
    var expiresDate:NSDate?
//    var remind_in:String?
    //当前授权用户ID
    var uid:String?
    //登录名
    var screen_name: String?
    //头像大图
    var avatar_large: String?
    init(dict:[String:AnyObject]) {
        super.init()
        setValuesForKeysWithDictionary(dict)
    }
    override func setValue(value: AnyObject?, forUndefinedKey key: String) {}
    override var description: String {
        
        let keys = ["access_token", "expires_in", "uid", "screen_name", "avatar_large"]
        
        return dictionaryWithValuesForKeys(keys).description
    }
//    ///保存用户信息
//    func saveUserAccount(){
//    var path=NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true).last!
//        path=(path as NSString).stringByAppendingPathComponent("account.plist")
//        print(path)
//        NSKeyedArchiver.archiveRootObject(self, toFile: path)
//    }
    ///归档
    func encodeWithCoder(aCoder:NSCoder){
    aCoder.encodeObject(access_token,forKey: "access_token")
        aCoder.encodeObject(expiresDate,forKey: "expiresDate")
        aCoder.encodeObject(uid,forKey: "uid")
        aCoder.encodeObject(screen_name,forKey: "screen_name")
        aCoder.encodeObject(avatar_large,forKey: "avatar_large")
    }
    ///解档
    required init?(coder aDecoder:NSCoder) {
        access_token=aDecoder.decodeObjectForKey("access_token") as? String
        expiresDate=aDecoder.decodeObjectForKey("expiresDate") as? NSDate
        screen_name=aDecoder.decodeObjectForKey("screen_name") as? String
        avatar_large=aDecoder.decodeObjectForKey("avatar_large")as? String
    }
}












